package com.koron.css2.newProjectCenter.impl.pubUpTime;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PubUpTimeInstance;
import com.koron.css2.newProjectCenter.bean.PubUpTimeInstance;
import com.koron.css2.newProjectCenter.bean.query.PubUpTimeInstanceQuery;
import com.koron.css2.newProjectCenter.mapper.PubUpTimeInstanceMapper;
import com.koron.css2.newProjectCenter.mapper.PubUpTimeInstanceMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 流程实例自定义时间
 *
 * @author cd
 */
public class PubUpTimeInstanceList implements ServerInterface {

    @Override
    @SuppressWarnings("rawtypes")
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            //将入参转换为QueryBean
            PubUpTimeInstanceQuery queryBean = JsonUtils.objectToPojo(req.getData(), PubUpTimeInstanceQuery.class);

            //获取Mapper实例
            PubUpTimeInstanceMapper mapper = factory.getMapper(PubUpTimeInstanceMapper.class);
            if (queryBean.getToDo() != null && queryBean.getToDo() == 2) {
                return save (mapper, userInfo, queryBean);
            }
            if (queryBean.getToDo() != null && queryBean.getToDo() == 3) {
                return getdetails(mapper, queryBean);
            }
            if (queryBean.getToDo() != null && queryBean.getToDo() == 4) {
                return delect(mapper, queryBean);
            }
            //设置分页参数
            PageHelper.startPage(queryBean.getPage(), queryBean.getPageCount());
            //执行SQL
            List<PubUpTimeInstance> list = mapper.selecList(queryBean);

            //输出分页结果
            MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
            info.setData(new PageInfo<>(list));
            return info;
        } catch (Exception e) {
            factory.close(false);
            logger.error("查询失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
        }
    }

    private MessageBean<?> delect(PubUpTimeInstanceMapper thisMapper, PubUpTimeInstanceQuery queryBean) {
        if (StringUtils.isEmpty(queryBean.getId())) {
            return MessageBean.create(Constant.NOT_NULL, "主键不能为空", null);
        }
        MessageBean<Integer> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "删除成功", Integer.class);
        Integer num =  thisMapper.delete(queryBean.getId());
        info.setData(num);
        return info;
    }

    private MessageBean<?> getdetails(PubUpTimeInstanceMapper thisMapper, PubUpTimeInstanceQuery queryBean) {
        if (StringUtils.isEmpty(queryBean.getId())) {
            return MessageBean.create(Constant.NOT_NULL, "主键不能为空", null);
        }
        MessageBean<PubUpTimeInstance> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PubUpTimeInstance.class);
        PubUpTimeInstance num =  thisMapper.getdetails(queryBean.getId());
        info.setData(num);
        return info;
    }

    private MessageBean<?> save(PubUpTimeInstanceMapper thisMapper, UserInfoBean userInfo, PubUpTimeInstanceQuery queryBean) {
        if (StringUtils.isEmpty(queryBean.getId())) {
            queryBean.setCreateAccount(userInfo.getUserInfo().getAcount());
            queryBean.setCreateTime(new DateTime());
            queryBean.setCreateName(userInfo.getUserInfo().getName());
            queryBean.setTenantId(userInfo.getCurWaterCode());
            queryBean.setId(Tools.getObjectId());
            queryBean.setStatus(0);
        } else {
            queryBean.setUpdateAccount(userInfo.getUserInfo().getAcount());
            queryBean.setUpdateTime(new DateTime());
            queryBean.setUpdateName(userInfo.getUserInfo().getName());
            queryBean.setStatus(1);
        }
        int num = thisMapper.updateOrSave(queryBean);

        MessageBean<Integer> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", Integer.class);
        info.setData(num);
        return info;
    }

}
